<style>
  .header-font {
    font-size: 15px;
    font-weight: bold;
  }
  .line {
    margin: 10px 0;
    width: 100%;
    border: 0.5px solid rgb(218, 218, 218);
  }
  .table {
    margin-top: 10px;
  }
  .paging {
    float:right;
    margin-top:10px;
  }
</style>
<template>
  <div>
    <div class='header-font'>
      <Icon type="md-apps" />
      <span>分组发送</span>
    </div>
    <div class="line"></div>
    <Button
      style="margin-right: 5px"
      icon="md-add"
      type="primary"
      @click="handleAddGroup">
      添加分组
    </Button>
    <Button
      icon="md-send"
      type="primary"
      @click="handleClick">
      确认发送
    </Button>
    <sms-send-drawer
      @sms-send-valid="handleEdit">
    </sms-send-drawer>
    <sms-send-confirm
      :confirmData="confirmData"
      @confirm-valid="handleConfirm">
    </sms-send-confirm>
    <delete-modal
      @delete-valid="handleConfirmDelete">
    </delete-modal>
    <team-detail-drawer></team-detail-drawer>
    <add-group-drawer @group-valid="handleSubmit"></add-group-drawer>
    <add-group-confirm @add-confirm-valid="handleConfirmAdd" :confirmData="confirmAddData"></add-group-confirm>
    <Table
      class="table"
      :columns="columns"
      :data="historyData"
      :loading="loading"
      border>
    </Table>
    <table-page
      class="paging"
      :dataCount="dataCount"
      @change-page="changePage"
      @change-page-size="changePageSize"
      :pageSize="pageSize">
    </table-page>
  </div>
</template>
<script>
import {
  mapActions,
  mapState
} from 'vuex'
import TablePage from '_c/table-page'
import SmsSendDrawer from '_c/sms-send-drawer'
import SmsSendConfirm from '_c/sms-send-confirm'
import DeleteModal from '_c/delete-modal'
import TeamDetailDrawer from '_c/team-detail-drawer'
import AddGroupDrawer from '_c/add-group-drawer'
import AddGroupConfirm from '_c/add-group-confirm'
export default {
  components: {
    SmsSendDrawer,
    SmsSendConfirm,
    DeleteModal,
    TeamDetailDrawer,
    AddGroupDrawer,
    AddGroupConfirm,
    TablePage
  },
  data () {
    return {
      page: 1,
      title: '短信管理分组发送',
      confirmData: {},
      confirmAddData: {},
      postAddData: {},
      selectData: [],
      currentid: 0,
      postid: null,
      groupData: null,
      rows: 20,
      loading: true,
      index: '',
      columns: [
        {
          title: '选择',
          key: 'chose',
          width: 70,
          align: 'center',
          render: (h, params) => {
            let id = params.row.id
            let flag = false
            if (this.currentid === id) {
              flag = true
            } else {
              flag = false
            }
            let self = this
            return h('div', [
              h('Radio', {
                props: {
                  value: flag
                },
                style: {
                  marginLeft: '10px'
                },
                on: {
                  'on-change': () => {
                    self.currentid = id
                    this.id_jishi = params.row.id
                    this.handleSelect(params.row.id)
                  }
                }
              })
            ])
          }
        },
        {
          title: '序号',
          type: 'index2',
          width: 80,
          align: 'center',
          render: (h, params) => {
            return h('span', params.index + (this.page - 1) * this.pageSize + 1)
          }
        },
        {
          title: '标签名称',
          key: 'srname',
          minWidth: 200,
          align: 'center'
        },
        {
          title: '内部销售',
          key: 'mname',
          minWidth: 150,
          align: 'center',
          render: (h, params) => {
            return h('span', params.row.mname ? params.row.mname : '全部')
          }
        },
        {
          title: '跟踪状态',
          key: 'state0',
          minWidth: 150,
          align: 'center',
          render: (h, params) => {
            return h('span', params.row.state0 === 1 ? '流失' : params.row.state0 === 0 ? '有效' : params.row.state0 === -1 ? '全部' : '-')
          }
        },
        {
          title: '客户状态',
          key: 'state',
          minWidth: 150,
          align: 'center',
          render: (h, params) => {
            return h('span', params.row.state === 0 ? '未预约' : params.row.state === 1 ? '已预约' : params.row.state === 2 ? '未到访' : params.row.state === 3 ? '已到店' : params.row.state === 4 ? '已签约' : params.row.state === 5 ? '已退款' : params.row.state === -1 ? '全部' : '-')
          }
        },
        {
          title: '签约金额',
          key: 'tprice',
          minWidth: 150,
          align: 'center',
          render: (h, params) => {
            return h('span', params.row.tprice ? params.row.tprice : '-')
          }
        },
        {
          title: '创建日期',
          key: 'ctime',
          minWidth: 150,
          align: 'center'
        },
        {
          title: '加入日期',
          key: 'joindate',
          minWidth: 180,
          align: 'center',
          render: (h, params) => {
            return h('span', params.row.joindate ? params.row.joindate : '-')
          }
        },
        {
          title: '操作',
          key: 'cz',
          minWidth: 196,
          align: 'center',
          render: (h, params) => {
            return h('div', [
              h('Button', {
                props: {
                  type: 'error',
                  size: 'small',
                  ghost: 'ghost'
                },
                style: {
                  marginRight: '5px'
                },
                on: {
                  click: () => {
                    this.handleDelete(params.row.id)
                  }
                }
              }, '删除'),
              h('Button', {
                props: {
                  type: 'primary',
                  size: 'small',
                  ghost: 'ghost'
                },
                style: {
                  marginRight: '5px'
                },
                on: {
                  click: () => {
                    this.handleDetail(params.row)
                  }
                }
              }, '详情')
            ])
          }
        }
      ]
    }
  },
  computed: {
    ...mapState({
      teamSendList: state => state.teamSend.teamSendList,
      dataCount: state => state.teamSend.teamSendDataCount,
      historyData: state => state.teamSend.teamSendHistoryData,
      pageSize: state => state.teamSend.teamSendPageSize,
      groupDetailList: state => state.groupDetail.groupDetailList

    })
  },
  methods: {
    ...mapActions([
      'getTeamSendList',
      'getGoodsList',
      'getNameList',
      'getGroupDetailList',
      'getSmsTemplateList',
      'sendSmsSendData',
      'editTeamSendData',
      'deleteTeamSendData',
      'getTeamSendCurrentRow',
      'updateCustomerDrawerState',
      'updateConfirmModalState',
      'updateTeamDrawerState',
      'updateAddGroupDrawerState',
      'updateAddGroupConfirmState',
      'updateDeleteModalState',
      'changeTeamSendPage',
      'changeTeamSendPageSize'
    ]),
    handleSubmit (playload) {
      this.confirmAddData = playload
      this.updateAddGroupConfirmState(true)
      this.updateAddGroupDrawerState(false)
    },
    async handleConfirmAdd () {
      if ((this.confirmAddData.state0 !== '' || this.confirmAddData.state !== '') && this.confirmAddData.repeat === '') {
        this.postAddData = {
          srname: this.confirmAddData.srname,
          joindate: `${this.confirmAddData.joindate[0]} ${this.confirmAddData.joindate[1]}`,
          uid: this.confirmAddData.uid,
          state0: this.confirmAddData.state0,
          state: this.confirmAddData.state,
          tprice: this.confirmAddData.tprice,
          gid: this.confirmAddData.gid,
          sex: this.confirmAddData.sex
        }
      }
      if (this.confirmAddData.state0 === '' && this.confirmAddData.repeat === '') {
        this.postAddData = {
          srname: this.confirmAddData.srname,
          joindate: `${this.confirmAddData.joindate[0]} ${this.confirmAddData.joindate[1]}`,
          uid: this.confirmAddData.uid,
          // state0: this.confirmAddData.state0,
          state: this.confirmAddData.state,
          tprice: this.confirmAddData.tprice,
          gid: this.confirmAddData.gid,
          sex: this.confirmAddData.sex
        }
      }
      if (this.confirmAddData.state === '' && this.confirmAddData.repeat === '') {
        this.postAddData = {
          srname: this.confirmAddData.srname,
          joindate: `${this.confirmAddData.joindate[0]} ${this.confirmAddData.joindate[1]}`,
          uid: this.confirmAddData.uid,
          state0: this.confirmAddData.state0,
          // state: this.confirmAddData.state,
          tprice: this.confirmAddData.tprice,
          gid: this.confirmAddData.gid,
          sex: this.confirmAddData.sex
        }
      }
      if (this.confirmAddData.state === '' && this.confirmAddData.state0 === '' && this.confirmAddData.repeat === '') {
        this.postAddData = {
          srname: this.confirmAddData.srname,
          joindate: `${this.confirmAddData.joindate[0]} ${this.confirmAddData.joindate[1]}`,
          uid: this.confirmAddData.uid,
          // state0: this.confirmAddData.state0,
          // state: this.confirmAddData.state,
          tprice: this.confirmAddData.tprice,
          gid: this.confirmAddData.gid,
          sex: this.confirmAddData.sex
        }
      }
      if (this.confirmAddData.repeat === 3) {
        this.postAddData = {
          srname: this.confirmAddData.srname,
          joindate: `${this.confirmAddData.joindate[0]} ${this.confirmAddData.joindate[1]}`,
          uid: this.confirmAddData.uid,
          state0: this.confirmAddData.state0,
          state: this.confirmAddData.state,
          tprice: this.confirmAddData.tprice,
          times: `${this.confirmAddData.repeat}=${this.confirmAddData.count}`,
          gid: this.confirmAddData.gid,
          sex: this.confirmAddData.sex
        }
      }
      if (this.confirmAddData.repeat === 4) {
        this.postAddData = {
          srname: this.confirmAddData.srname,
          joindate: `${this.confirmAddData.joindate[0]} ${this.confirmAddData.joindate[1]}`,
          uid: this.confirmAddData.uid,
          state0: this.confirmAddData.state0,
          state: this.confirmAddData.state,
          tprice: this.confirmAddData.tprice,
          times: `${this.confirmAddData.repeat}=${this.confirmAddData.count}`,
          gid: this.confirmAddData.gid,
          sex: this.confirmAddData.sex
        }
      }
      this.loading = true
      const resp = await this.editTeamSendData(this.postAddData)
      if (resp.data.state === '000') {
        this.$Message.success('添加分组成功')
        this.getTeamSendList({
          rows: this.rows,
          page: this.page
        }).then((res) => {
          if (res.data.state === '000') {
            this.loading = false
          }
        })
      } else {
        this.$Message.error(resp.data.msg)
      }
      this.updateAddGroupConfirmState(false)
      this.confirmAddData.addGroupDrawer.resetFields()
    },
    handleClick () {
      this.updateCustomerDrawerState(true)
    },
    handleDetail (row) {
      this.getTeamSendCurrentRow(row)
      this.groupData = {
        joindate: row.joindate,
        uid: row.uid && row.uid !== 0 ? row.uid : '',
        state: row.state !== -1 ? row.state : '',
        state0: row.state0 !== -1 ? row.state0 : '',
        tprice: row.tprice,
        times: row.times
      }
      this.updateTeamDrawerState(true)
      this.getGroupDetailList(this.groupData)
    },
    handleAddGroup () {
      this.updateAddGroupDrawerState(true)
    },
    handleEdit (playload, id) {
      this.confirmData = playload
      this.updateCustomerDrawerState(false)
      this.updateConfirmModalState(true)
    },
    async handleConfirm () {
      this.postData = {
        smsid: this.confirmData.id,
        name: this.confirmData.tag,
        roleid: this.postid
      }
      const resp = await this.sendSmsSendData(this.postData)
      if (resp.data.state === '000') {
        this.$Message.success('发送成功')
      } else {
        this.$Message.error(resp.data.msg)
      }
      this.updateConfirmModalState(false)
    },
    handleSelect (id) {
      this.postid = id
    },
    changePage (index) {
      this.loading = true
      this.page = index
      this.changeTeamSendPage(index)
      this.getTeamSendList({
        page: index,
        rows: this.rows
      }).then((res) => {
        if (res.data.state === '000') {
          this.loading = false
        }
      })
    },
    changePageSize (value) {
      this.loading = true
      this.rows = value
      this.changeTeamSendPageSize(value)
      this.changePage(this.page)
      this.getTeamSendList({
        rows: value
      }).then((res) => {
        if (res.data.state === '000') {
          this.loading = false
        }
      })
    },
    handleDelete (id) {
      this.id = id
      this.updateDeleteModalState(true)
    },
    async handleConfirmDelete () {
      this.loading = true
      const resp = await this.deleteTeamSendData({ id: this.id })
      if (resp.data.state === '000') {
        this.$Message.success('删除成功')
        this.getTeamSendList({
          page: this.page,
          rows: this.rows
        }).then((res) => {
          if (res.data.state === '000') {
            this.loading = false
          }
        })
      } else {
        this.$Message.error('删除失败')
      }
      this.updateDeleteModalState(false)
      this.changePage(this.page)
    }
  },
  created () {
    this.getTeamSendList({}).then((res) => {
      if (res.data.state === '000') {
        this.loading = false
      }
    })
    this.getSmsTemplateList({
      state: 1
    })
    this.getNameList({})
    this.getGoodsList({})
  }
}
</script>
